﻿// <copyright file="MainForm.Splint.cs" company="Horiba Medical">
//     Copyright (c) Horiba Medical. All rights reserved.
// </copyright>
// <summary>
//     Splint part of the main form.
// </summary>

namespace DoxyOutParser.Forms
{
	using System;
	using System.ComponentModel;
	using System.IO;
	using System.Windows.Forms;
	using DoxyOutParser.Launcher;
	using DoxyOutParser.Parser;
	using DoxyOutParser.WorkingSet;

	/// <summary>
	/// Splint part of the MainForm class code.
	/// </summary>
	public partial class MainForm
	{
		/// <summary>
		/// Action taken when clicking on the combo box dropdown button.
		/// </summary>
		/// <param name="sender">Event sender.</param>
		/// <param name="e">Event arguments.</param>
		private void SplintToolStripComboBoxDropDown(object sender, EventArgs e)
		{
			if (this.splintToolStripComboBox.Items.Count == 0)
			{
				PopulateComboBoxWithProjectFiles(this.splintToolStripComboBox, user.Default.splintWorkingDir);
			}
		}

		#region Buttons

		/// <summary>
		/// Action taken when clicking on the splintToolStripButtonRun element.
		/// </summary>
		/// <param name="sender">Event sender.</param>
		/// <param name="e">Event arguments.</param>
		private void SplintToolStripButtonRun_Click(object sender, EventArgs e)
		{
			string workingDirectory = this.splintToolStripTextBoxBrowse.Text;

			try
			{
				// Generate the doxygen warning log file
				string splintBinary = @"c:\cygwin\bin\splint";
				WorkSet project = (WorkSet)this.splintToolStripComboBox.SelectedItem;

				SplintLauncher.Launch(project, workingDirectory, splintBinary);
			}
			catch (FileNotFoundException)
			{
				InfoForm infoBox = new InfoForm("The doxyfile corresponding to your project could not be found, you should choose another directory.", "Warning");
				infoBox.Show();
				this.DoxygenToolStripButtonBrowse_Click(sender, e);
			}
			catch (Win32Exception)
			{
				InfoForm infoBox = new InfoForm("The directory you provided is not valid, please choose another one.", "Warning");
				infoBox.Show();
			}
			catch (NullReferenceException)
			{
				InfoForm infoBox = new InfoForm("A project file must be specified, please use the dropdown button on the combobox to select one.", "Warning");
				infoBox.Show();
			}
		}

		/// <summary>
		/// Action taken when clicking on the splintToolStripButtonBrowse element.
		/// </summary>
		/// <param name="sender">Event sender.</param>
		/// <param name="e">Event arguments.</param>
		private void SplintToolStripButtonBrowse_Click(object sender, EventArgs e)
		{
			FolderBrowserDialog chosenRootDir = new FolderBrowserDialog();

			chosenRootDir.RootFolder = System.Environment.SpecialFolder.MyComputer;
			chosenRootDir.Description = "Please select where the project root is located:";

			if (chosenRootDir.ShowDialog() == DialogResult.OK)
			{
				////this.Trace("The directory "+ chosenRootDir.SelectedPath +" has been selected");
			}

			try
			{
				this.splintToolStripTextBoxBrowse.Text = chosenRootDir.SelectedPath;
				PopulateComboBoxWithProjectFiles(this.splintToolStripComboBox, chosenRootDir.SelectedPath);

				user.Default.splintWorkingDir = chosenRootDir.SelectedPath;
				user.Default.Save();
			}
			catch (ArgumentException exception)
			{
				InfoForm infoBox = new InfoForm(exception.Message, "Exception occured");
				infoBox.Show();
			}
		}

		/// <summary>
		/// Action taken when clicking on the splintToolStripButtonParse element.
		/// </summary>
		/// <param name="sender">Event sender.</param>
		/// <param name="e">Event arguments.</param>
		private void SplintToolStripButtonParse_Click(object sender, EventArgs e)
		{
			try
			{
				WorkSet project = (WorkSet)this.splintToolStripComboBox.SelectedItem;

				// Parse the splint warning log file
				this.splintEntryList = new SplintOutputParser().Parse(project.RootDir + @"\splint.warning.log", this.splintToolStripTextBoxFilter.Text);
				this.splintEntryDataGridView.DataSource = this.splintEntryList;

				// Automatically resize the visible rows.
				this.splintEntryDataGridView.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;

				// Check for errors & display if any
				SplintOutputParser.ParseAndDisplayErrors(project.RootDir + @"\splint.error.log");
			}
			catch (FileNotFoundException)
			{
				InfoForm infoBox = new InfoForm("The logs could not be found, either change directory or run Splint.", "Warning");
				infoBox.Show();
			}
			catch (NullReferenceException)
			{
				InfoForm infoBox = new InfoForm("A project file must be specified, please use the dropdown button on the combobox to select one.", "Warning");
				infoBox.Show();
			}
		}

		#endregion

		#region TextBoxes

		/// <summary>
		/// Action taken when changing the text on the splintToolStripTextBoxBrowse element.
		/// </summary>
		/// <param name="sender">Event sender.</param>
		/// <param name="e">Event arguments.</param>
		private void SplintToolStripTextBoxBrowse_TextChanged(object sender, EventArgs e)
		{
			user.Default.splintWorkingDir = this.splintToolStripTextBoxBrowse.Text;
			user.Default.Save();
		}

		/// <summary>
		/// Action taken when double clicking on the SplintToolStripTextBoxBrowse element.
		/// </summary>
		/// <param name="sender">Event sender.</param>
		/// <param name="e">Event arguments.</param>
		private void SplintToolStripTextBoxBrowse_DoubleClick(object sender, EventArgs e)
		{
			this.SplintToolStripButtonBrowse_Click(sender, e);
			PopulateComboBoxWithProjectFiles(this.splintToolStripComboBox, user.Default.splintWorkingDir);
		}

		/// <summary>
		/// Action taken when validating the SplintToolStripTextBoxBrowse element.
		/// </summary>
		/// <param name="sender">Event sender.</param>
		/// <param name="e">Event arguments.</param>
		private void SplintToolStripTextBoxBrowse_Validated(object sender, EventArgs e)
		{
			/*while (Directory.Exists(this.splintToolStripTextBoxBrowse.Text) == false)
			{
				InfoForm infoBox = new InfoForm("This is not a valid directory ...");
				infoBox.Show();
				this.SplintToolStripButtonBrowse_Click(sender, e);
			}

			PopulateComboBoxWithProjectFiles(this.splintToolStripComboBox, this.splintToolStripTextBoxBrowse.Text);*/
		}

		/// <summary>
		/// Action taken when clicking on the filter button of the Splint toolstrip.
		/// </summary>
		/// <param name="sender">Event sender.</param>
		/// <param name="e">Event arguments.</param>
		private void SplintToolStripButtonFilter_Click(object sender, EventArgs e)
		{
			try
			{
				WorkSet project = (WorkSet)this.splintToolStripComboBox.SelectedItem;

				// Parse the splint warning log file
				this.splintEntryList = new SplintOutputParser().Parse(project.RootDir + @"\splint.warning.log", this.splintToolStripTextBoxFilter.Text);

				this.splintEntryList.Sort();
				this.splintEntryDataGridView.DataSource = this.splintEntryList;

				// Automatically resize the visible rows.
				this.splintEntryDataGridView.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
			}
			catch (FileNotFoundException)
			{
				InfoForm infoBox = new InfoForm("The logs could not be found, either change directory or run Splint.", "Warning");
				infoBox.Show();
			}
			catch (NullReferenceException)
			{
				InfoForm infoBox = new InfoForm("A project file must be specified, please use the dropdown button on the combobox to select one.", "Warning");
				infoBox.Show();
			}
		}


		private void hideLibtoolStripButton_Click(object sender, EventArgs e)
		{
			this.SplintToolStripButtonFilter_Click(sender, e);
		}

		/// <summary>
		/// Action taken when pressing a key on the filter textbox of the Splint toolstrip.
		/// </summary>
		/// <param name="sender">Event sender.</param>
		/// <param name="e">Event arguments.</param>
		private void SplintToolStripTextBoxFilter_KeyPress(object sender, KeyPressEventArgs e)
		{
			this.SplintToolStripButtonFilter_Click(sender, e);
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void SplintToolStripTextBoxFilter_TextChanged(object sender, EventArgs e)
		{
			this.SplintToolStripButtonFilter_Click(sender, e);
		}
		#endregion

		#region EntryDataGrid

		/// <summary>
		/// Action taken when double clicking on the row header of the Splint entry data grid.
		/// </summary>
		/// <param name="sender">Event sender.</param>
		/// <param name="e">Event arguments.</param>
		private void SplintEntryDataGridView_RowHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
		{
			WorkSet project = (WorkSet)this.splintToolStripComboBox.SelectedItem;

			if (this.splintEntryDataGridView.SelectedRows.Count == 1)
			{
				OutputEntry entryInCell = (OutputEntry)this.splintEntryDataGridView.SelectedRows[0].DataBoundItem;
				try
				{
					if (this.radioButtonNotepadPP.Checked)
					{
						NotepadPPLauncher.Launch(project.RootDir + "\\" + entryInCell.FileNameAndPath, entryInCell.Line);
					}
					else if (this.radioButtonTextpad.Checked)
					{
						TextPadLauncher.Launch(project.RootDir + "\\" + entryInCell.FileNameAndPath, entryInCell.Line);
					}
					else if (this.radioButtonSourceInsight.Checked)
					{
						SourceInsightLauncher.Launch(project.RootDir + "\\" + entryInCell.FileNameAndPath, entryInCell.Line);
					}
				}
				catch (Win32Exception exception)
				{
					InfoForm infoBox = new InfoForm(exception.Message, "Exception occured");
					infoBox.Show();
				}
			}
			else
			{
				InfoForm infoBox = new InfoForm("There is nothing to open, try parsing a warning log first.", "Warning");
				infoBox.Show();
			}
		}

		/// <summary>
		/// Action taken when double clicking on the column header of the Splint entry data grid.
		/// </summary>
		/// <param name="sender">Event sender.</param>
		/// <param name="e">Event arguments.</param>
		private void SplintEntryDataGridView_ColumnHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
		{
			this.splintEntryList.Sort();
		}

		#endregion
	}
}
